package net.gz01.wallet.web.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Channel;
import net.gz01.wallet.web.model.Dictionary;
import net.gz01.wallet.web.model.FeeModel;
import net.gz01.wallet.web.model.Merch;
import net.gz01.wallet.web.model.Org;
import net.gz01.wallet.web.model.ShareFee;

public class ShareFeeController extends BaseController {

	public void search() {
		render("fee/shareFeeList.html");
	}
	
	public void listJson() {
		ZOSearchCondition sc = super.getSearchCondition();
		Page<ShareFee> pageResult = ShareFee.dao.search(sc);
		renderJson(new PageRespon(pageResult));
	}

	public void shareFeeAdd() {
		setAttr("orgList", Org.dao.selectAll());
		setAttr("feeModelList", FeeModel.dao.selectAll());
		setAttr("channelList", Channel.dao.selectAll());
		setAttr("payTypeList", Dictionary.dao.selectBizType());
		render("fee/shareFeeAdd.html");
	}
	
	public void costFeeAdd() {
		setAttr("orgList", Org.dao.selectAll());
		setAttr("feeModelList", FeeModel.dao.selectAll());
		setAttr("channelList", Channel.dao.selectAll());
		setAttr("payTypeList", Dictionary.dao.selectBizType());
		render("fee/costFeeAdd.html");
	}

	public void updatePage() {
		ShareFee shareFee = ShareFee.dao.findById(getPara("id"));
		setAttr("shareFee", shareFee);
		setAttr("orgList", Org.dao.selectAll());
		setAttr("feeModelList", FeeModel.dao.selectAll());
		setAttr("channelList", Channel.dao.selectAll());
		setAttr("merchList", Merch.dao.selectToRedis());
		setAttr("payTypeList", Dictionary.dao.selectBizType());
		render("fee/shareFeeUpdate.html");
	}

	@Before(Tx.class)
	public void add() {
			ShareFee shareFee = getModel(ShareFee.class, true);
			ShareFee s = null;
			if (shareFee.getIsFixed()) {
				s = ShareFee.dao.selectIsFixed(shareFee.getMerchCode(), shareFee.getChannelCode(), shareFee.getOrgCode(), shareFee.getPayTypeCode());
			} else {
				s = ShareFee.dao.selectIsNotFixed(shareFee.getChannelCode(), shareFee.getOrgCode(), shareFee.getPayTypeCode());
			}
			if(s != null) {
				renderFail("分润费率已经存在");
				return;
			}
			shareFee.save();
			renderSuccess();
	}

	@Before(Tx.class)
	public void update() {
		ShareFee shareFee = getModel(ShareFee.class, true);
		shareFee.update();
		renderSuccess();
	}

	@Before(Tx.class)
	public void delete() {
		ShareFee s = ShareFee.dao.findById(getPara("id"));
		ShareFee.dao.deleteById(s.getId());
		renderSuccess(false);
	}

}